package com.scorpio.system.service.impl;

import com.scorpio.common.annotation.DataSource;
import com.scorpio.common.constant.Constants;
import com.scorpio.common.enums.DataSourceType;
import com.scorpio.common.service.impl.BaseServiceImpl;
import com.scorpio.common.utils.ConvertUtils;
import com.scorpio.common.utils.treenode.TreeNodeUtil;
import com.scorpio.redis.RedisCache;
import com.scorpio.system.domain.AreaInfoEntity;
import com.scorpio.system.dto.AreaInfoDTO;
import com.scorpio.system.mapper.AreaInfoMapper;
import com.scorpio.system.service.AreaInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@DataSource(DataSourceType.SLAVE)
public class AreaInfoServiceImpl extends BaseServiceImpl<AreaInfoMapper, AreaInfoEntity> implements AreaInfoService {

    @Autowired
    private RedisCache redisCache;

    @Override
    public List<AreaInfoDTO> treeselect() {
        List<AreaInfoDTO> list = redisCache.getCacheObject(Constants.CACHE_AREAINFO);
        if (list == null) {
            List<AreaInfoEntity> entities = baseDao.selectList(getQueryWrapper());
            list = TreeNodeUtil.bulid(ConvertUtils.sourceToTarget(entities, AreaInfoDTO.class), 0l, 3);
            redisCache.setCacheObject(Constants.CACHE_AREAINFO, list);
        }
        return list;
    }

    @Override
    public AreaInfoEntity selectById(Long id) {
        return baseDao.selectById(id);
    }

    @Override
    public AreaInfoEntity selectByCode(String areaCode) {
        return baseDao.selectOne(getQueryWrapper().eq("area_code", areaCode));
    }

    @Override
    public AreaInfoEntity selectByCodeLevel(String areaCode, Integer areaLevel) {
        return baseDao.selectOne(getQueryWrapper().eq("area_code", areaCode).eq("area_level", areaLevel));
    }

}
